import axios, { type AxiosInstance, type InternalAxiosRequestConfig, type AxiosResponse } from 'axios'
const BASE_URL = import.meta.env.VITE_BASE_URL
const request: AxiosInstance = axios.create({
    baseURL: BASE_URL,
    timeout: 10000
})
request.interceptors.request.use((config: InternalAxiosRequestConfig) => {
    //兼容文件上传
    config.headers['Content-Type'] = config.data instanceof FormData ? 'multipart/form-data' : 'application/json;charset=utf-8'
    window.$isLoading.value = true
    return config
}, () => {
    window.$isLoading.value = false
    window.$message.error('请求失败')
    return Promise.reject
})
request.interceptors.response.use((res: AxiosResponse) => {
    window.$isLoading.value = false
    return res.data
}, (err) => {
    window.$isLoading.value = false
    window.$message.error(err.message)
    return Promise.reject
})
export default request